<?php
namespace jy_robshop;
defined('BY_JYA') or exit('error');
/*
	机器人
*/
class ai extends \plugin{
	/*
		自动抢购
	*/
	public function up_canlog($yaoid=0,$isexi=false){
			if(!$yaoid){
				return false;
			}
			$yao=pdo_get('jy_robshop_yao',array('id'=>$yaoid,'uniacid'=>$_SESSION['uniacid']));
			if(!$yao){
				return false;
			}
			$aisets=pdo_getall('jy_robshop_aiset',array('status'=>1,'cangid'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
			foreach($aisets as $item){
				$jifen1=0;
				if($item['baojifen']){
					$jifen1	+=$item['baojifen'];
				}
				if($item['aijifen']){
					$jifen1	+=$item['aijifen'];
				}
				if($item['jian_xiaojinum']){
					$jifen1	+=$item['jian_xiaojinum'];
				}
				$jifen=$item['onefee']+$item['addfee']*($yao['paixu']-$item['paixu']);
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$yao['id'],
					'aiid'=>$item['id'],
					'jifen'=>$jifen,
					'jifen1'=>$jifen1,
					'baobei'=>$item['baobei'],
					'dayid'=>date('Y-m-d'),
					'createtime'=>TIMESTAMP,
					'status'=>0,
				);
				$this->insert('canlog',$data);
				$canlogid=pdo_insertid();
				$is_success=$this->model('model')->up_success($yao['yaoid']);
				$data['fee']=$data['jifen'];
				$data['canlogid']=$canlogid;
				$this->model('model')->add_feelog($data,15);
				$this->model('model')->up_guo2dou($data);
				if($is_success){
					break;
				}
			}
			return true;
	}
	//预约抢购
	public function up_yuelog($yaoid=0,$isexi=false){
			if(!$yaoid){
				return false;
			}
			$yao=pdo_get('jy_robshop_yao',array('id'=>$yaoid,'uniacid'=>$_SESSION['uniacid']));
			if(!$yao){
				return false;
			}
			$yuelogs=pdo_getall('jy_robshop_yuelog',array('status'=>0,'yaoid'=>$yaoid,'uniacid'=>$_SESSION['uniacid']));
			foreach($yuelogs as $item){
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$item['yaoid'],
					'jifen'=>$item['jifen'],
					'baobei'=>$item['baobei'],
					'dayid'=>date('Y-m-d'),
					'createtime'=>TIMESTAMP,
					'status'=>0,
				);
				$this->insert('canlog',$data);
				$canlogid=pdo_insertid();
				$is_success=$this->model('model')->up_success($data['yaoid']);
				pdo_update('jy_robshop_yuelog',array('status'=>1),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				pdo_update('jy_robshop_feelog',array('canlogid'=>$canlogid),array('yuelogid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$data['canlogid']=$canlogid;
				$this->model('model')->up_guo2dou($data);
			}
			return true;
	}
	public function up_ailog($aiset){
		model('core/member')->member_credit($_SESSION['uid'],'credit4',$aiset['payfee'],false,'jy_robshop','机器人抢购');
		model('core/member')->member_credit($_SESSION['uid'],'credit1',$aiset['payjifen'],false,'jy_robshop','机器人抢购');
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$_SESSION['uid'],
					'cangid'=>$aiset['cangid'],
					'onefee'=>$aiset['onefee'],
					'addfee'=>$aiset['addfee'],
					'yaonum'=>$aiset['yaonum'],
					'baobei'=>$aiset['baobei'],
					'paixu'=>$aiset['paixu'],
					'baojifen'=>$aiset['baojifen'],
					'jian_xiaojinum'=>$aiset['jian_xiaojinum'],
					'aijifen'=>$aiset['aijifen'],
					'payfee'=>$aiset['payfee'],
					'payjifen'=>$aiset['payjifen'],
					'createtime'=>TIMESTAMP,
					'status'=>1,
		);
		$data['oneallfee']=$aiset['baobei']>1?$aiset['onefee']*$aiset['baobei']:$aiset['onefee'];
		$this->insert('aiset',$data);
		$canlogid=pdo_insertid();
		return true;
	}
}